package cn.kgc.springcloud.controller;

import cn.kgc.springcloud.entity.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @author 课工场
 * @date 2025/1/9
 * @description
 */
@RestController
@RequestMapping("consul")
public class ConsulController {

    @Value("${server.port}")
    private String port;

    @GetMapping("m1")
    public String method1(){

        // 模拟让程序阻塞5s时间  演示openfeign调用超时
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        return "invoke ConsulController method1 ok "+port ;
    }


    @GetMapping("m2")
    public String method2(Integer id){
        return "invoke ConsulController method2 ok "+port +"id = "+id ;
    }

    @GetMapping("m3")
    public String method3(Integer id,String info){
        return "invoke ConsulController method3 ok "+port +" id = "+id +"info="+info;
    }

    @GetMapping("m4/{id}")
    public String method4(@PathVariable("id") Integer id){
        return "invoke ConsulController method4 ok "+port +" id = "+id ;
    }

    // ?id=1&username=zs&password=123456
    @GetMapping("m5")
    public String method5(User user){
        return "invoke ConsulController method5 ok"+user ;
    }

    @PostMapping("m6")
    public String method6(@RequestBody User user){
        return "invoke ConsulController method6 ok"+user ;
    }

    @PostMapping("m7")
    public String method7(Integer[] ids){
        return "invoke ConsulController method7 ok"+ Arrays.toString(ids);
    }


    // json 字符串
    @PostMapping("m8")
    public User method8(){
        User user = new User();
        user.setId(10);
        user.setUsername("xiaohei");
        user.setPassword("111111");
        return user;
    }

    @PostMapping("m9")
    public List<User> method9(){
        User user = new User();
        user.setId(10);
        user.setUsername("xiaohei");
        user.setPassword("111111");

        List<User> list = Arrays.asList(user, user, user);
        return list;
    }

    @PostMapping("m10")
    public HashMap<String,Object> method10(){
        HashMap<String, Object> result = new HashMap<>();

        result.put("code",200);
        result.put("message","ok");
        result.put("data",new User());

        return  result;
    }


}
